import { Component, OnInit, Output, Input, EventEmitter } from '@angular/core';
import { NzMessageService } from 'ng-zorro-antd';
import { LocalStorage } from '@core/local.storage';
import { UploadChangeParam } from 'ng-zorro-antd/upload';
import { HttpClient } from '@angular/common/http';
import { environment } from '../../../../environments/environment';

@Component({
    selector: 'app-petitionoperamodal',
    templateUrl: './operaModal.component.html',
    styleUrls: ['./petitionView.component.less']
})
export class PetitionOperaModalComponent implements OnInit {
    @Input() apiUrl: any;
    @Input() operaType: any;
    @Input() width: any;
    @Input() responseData: any;
    @Input() popTitle: any;
    @Output() public closeModal = new EventEmitter();
    @Output() public reloadData = new EventEmitter();

    Visible = false;
    operaData: any = {};
    objData: any = {};
    thisData: any = { 'count': 0, 'id': [] };
    loginInfo = JSON.parse(this.ls.get('loginInfo'));

    sexList = [{ "value": "1", "label": "男" }, { "value": "0", "label": "女" }];
    nationList = [{ "value": "01", "label": "汉族" }, { "value": "02", "label": "蒙古族" }, { "value": "03", "label": "回族" },
    { "value": "04", "label": "藏族" }, { "value": "05", "label": "维吾尔族" }, { "value": "06", "label": "苗族" },
    { "value": "07", "label": "彝族" }, { "value": "08", "label": "壮族" }, { "value": "09", "label": "布依族" },
    { "value": "10", "label": "朝鲜族" }, { "value": "11", "label": "满族" }, { "value": "12", "label": "侗族" },
    { "value": "13", "label": "瑶族" }, { "value": "14", "label": "白族" }, { "value": "15", "label": "土家族" },
    { "value": "16", "label": "哈尼族" }, { "value": "17", "label": "哈萨克族" }, { "value": "18", "label": "傣族" },
    { "value": "19", "label": "黎族" }, { "value": "20", "label": "傈僳族" }, { "value": "21", "label": "佤族" },
    { "value": "22", "label": "畲族" }, { "value": "23", "label": "高山族" }, { "value": "24", "label": "拉祜族" },
    { "value": "25", "label": "水族" }, { "value": "26", "label": "东乡族" }, { "value": "27", "label": "纳西族" },
    { "value": "28", "label": "景颇族" }, { "value": "29", "label": "柯尔克孜族" }, { "value": "30", "label": "土族" },
    { "value": "31", "label": "达斡尔族" }, { "value": "32", "label": "仫佬族" }, { "value": "33", "label": "羌族" },
    { "value": "34", "label": "布朗族" }, { "value": "35", "label": "撒拉族" }, { "value": "36", "label": "毛难族" },
    { "value": "37", "label": "仡佬族" }, { "value": "38", "label": "锡伯族" }, { "value": "39", "label": "阿昌族" },
    { "value": "40", "label": "普米族" }, { "value": "41", "label": "塔吉克族" }, { "value": "42", "label": "怒族" },
    { "value": "43", "label": "乌孜别克族" }, { "value": "44", "label": "俄罗斯族" }, { "value": "45", "label": "鄂温克族" },
    { "value": "46", "label": "崩龙族" }, { "value": "47", "label": "保安族" }, { "value": "48", "label": "裕固族" },
    { "value": "49", "label": "京族" }, { "value": "50", "label": "塔塔尔族" }, { "value": "51", "label": "独龙族" },
    { "value": "52", "label": "鄂伦春族" }, { "value": "53", "label": "赫哲族" }, { "value": "54", "label": "门巴族" },
    { "value": "55", "label": "珞巴族" }, { "value": "56", "label": "基诺族" }];
    politicalList = [
        { "value": "1", "label": "中共党员" },
        { "value": "2", "label": "中共预备党员" },
        { "value": "3", "label": "共青团员" },
        { "value": "4", "label": "群众" }
    ];
    levelList = [
        { "value": "1", "label": "省部级" },
        { "value": "2", "label": "地厅级" },
        { "value": "3", "label": "县处级" },
        { "value": "4", "label": "乡科级" },
        { "value": "5", "label": "一般干部" },
        { "value": "6", "label": "其他" }
    ];

    uploadUrl = environment.devscore + '/letter/uploadFile';
    uploadFileList: any = [];

    constructor(
        private msg: NzMessageService,
        public ls: LocalStorage,
        private http: HttpClient
    ) { }

    ngOnInit() {
        if (!this.width) {
            this.width = '500';
        }
        switch (this.operaType) {
            case 'add':
                this.getApi(() => {
                    // to do
                });
                break;
            case 'edit':
                let editStatus = false;
                let editCount = 0;
                for (const item of this.responseData) {
                    if (item.checked) {
                        this.objData = item;
                        editCount += 1;
                        this.thisData.id.push(item.id);
                        editStatus = true;
                    }
                }
                if (editStatus && editCount === 1) {
                    this.getApi(() => {
                        this.operaData.addr = this.objData.addr;
                        this.operaData.informantCompany = this.objData.informantCompany;
                        this.operaData.informantDuries = this.objData.informantDuries;
                        this.operaData.informantName = this.objData.informantName;
                        this.operaData.informantPhone = this.objData.informantPhone;
                        this.operaData.informantSex = this.objData.informantSex;
                        this.operaData.idNumber = this.objData.idNumber;
                        this.operaData.nation = this.objData.nation;
                        this.operaData.politicalOutlook = this.objData.politicalOutlook;
                        this.operaData.reflectProblem = this.objData.reflectProblem;
                        this.operaData.reflectionPersionSex = this.objData.reflectionPersionSex;
                        this.operaData.reflectionPersonCompany = this.objData.reflectionPersonCompany;
                        this.operaData.reflectionPersonDuties = this.objData.reflectionPersonDuties;
                        this.operaData.reflectionPersonLevel = this.objData.reflectionPersonLevel;
                        this.operaData.reflectionPersonName = this.objData.reflectionPersonName;
                        if (this.objData.dataUrl) {
                            this.uploadFileList = [{
                                name: this.objData.dataUrl.split("/")[this.objData.dataUrl.split("/").length - 1],
                                url: this.objData.dataUrl
                            }]
                        }
                    });
                } else {
                    this.Cancel();
                    this.msg.error('请选择数据进行操作');
                }
                break;
            case 'del':
                let delStatus = false;
                for (const item of this.responseData) {
                    if (item.checked) {
                        this.objData = item;
                        this.thisData.count += 1;
                        this.thisData.id.push(item.id);
                        delStatus = true;
                    }
                }
                if (delStatus) {
                    this.getApi(() => {
                        // to do
                    });
                } else {
                    this.Cancel();
                    this.msg.error('请选择数据删除');
                }
                break;
        }
    }
    getApi(callback) {
        setTimeout(() => {
            this.Visible = true;
            if (callback) {
                callback()
            }
        }, 200);
    }
    handleChange(info: UploadChangeParam) {
        switch (info.file.status) {
            case 'done':
                const response = info.file.response;
                if (response) {
                    console.log(response.data[0].split("/")[response.data[0].split("/").length - 1])
                    console.log(response.data[0])
                    if (!response.code) {
                        this.uploadFileList.push({
                            name: response.data[0].split("/")[response.data[0].split("/").length - 1],
                            url: response.data[0]
                        })
                    } else {
                        this.msg.error(response.msg);
                    }
                }
                break;
            case 'removed':
                this.uploadFileList = [];
                break;
            case 'error':
                this.msg.error(`${info.file.name} 上传失败`);
                break;
        }
    }
    Ok() {
        const param: any = this.operaData;
        switch (this.operaType) {
            case 'add':
            case 'edit':
                if (!this.operaData.reflectionPersonName || !this.operaData.reflectionPersonCompany ||
                    !this.operaData.reflectionPersonLevel || !this.operaData.reflectProblem) {
                    this.msg.error('必填项不能为空', { nzDuration: 1000 });
                    return;
                }
                param.ip = '';
                param.dataUrl = this.uploadFileList.length ? this.uploadFileList[0].url : '';
                param.userId = this.loginInfo ? this.loginInfo.VC_NUMBER : '';
                if (this.operaType === 'edit') {
                    param.id = this.thisData.id.toString();
                }
                break;
            case 'del':
                param.ids = this.thisData.id.toString();
                break;
        }
        this.http.post(this.apiUrl, param).subscribe((res: any) => {
            if (!res.code) {
                this.Visible = false;
                setTimeout(() => {
                    this.closeModal.emit();
                    this.reloadData.emit();
                }, 200);
            } else {
                if (res.msg) {
                    this.msg.error(res.msg);
                } else {
                    this.msg.error('操作失败');
                }
            }
        });
    }
    Cancel() {
        this.Visible = false;
        setTimeout(() => {
            this.closeModal.emit();
        }, 200);
    }
}
